﻿using System;

namespace ProjetoComperio.Framework.Util
{
    /// <summary>
    /// This class provides some additional methods to String class
    /// </summary>
    public static class StringExtension
    {

        /// <summary>
        /// This method return true if this string given as parameter
        /// is parseable to DateTime, it is working with date in the following
        /// format: MM/DD/AAAA
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static Boolean IsParseableToDateTime(this String param)
        {
            DateTime tmp;
            return DateTime.TryParse(param, out tmp);
        }


        /// <summary>
        /// This method tries out to convert a string value in boolean
        /// This method accepts two formats:
        /// True/False and On/Off
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static Boolean IsParseableToBoolean(this String param)
        {
           

            Boolean tmp = false;
            //converting formats
            if (param.ToUpper() == "ON")
                param = "True";
            else if (param.ToUpper() == "OFF")
                param = "False";

            return Boolean.TryParse(param, out tmp);
        }

        /// <summary>
        /// This method parses a string to Boolean using two formats:
        /// True/False and On/Off
        /// </summary>
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static Boolean ParseToBoolean(this String param)
        {
            Boolean resul = false;
            ParseToBoolean(param, out resul);
            return resul;
        }


        private static bool ParseToBoolean(String param, out Boolean resul)
        {
            if (param.ToLower().Equals("on"))
                param = "true";
            else if (param.ToLower().Equals("off"))
                param = "false";

            return Boolean.TryParse(param, out resul);
        }
    }
}
